﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class PurchasePersonController : ControllerBase
    {

        private OriginalCostApp _originalCostApp;
        private TechnologyApp _technologyApp;
        private OperateRecordApp _operateRecordApp;
        private OrderProductApp _orderProductApp;
        private FinanceCostApp _financeCostApp;
        private PurchaseApp _purchaseApp;

        public PurchasePersonController()
        {
            _originalCostApp = new OriginalCostApp();
            _technologyApp = new TechnologyApp();
            _operateRecordApp = new OperateRecordApp();
            _orderProductApp = new OrderProductApp();
            _financeCostApp = new FinanceCostApp();
            _purchaseApp = new PurchaseApp();
        }

        // GET: PurchasePerson
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PurchaseQuery(int page = 1, string creatorName = "", string productName = "", string beginTime = "", string endTime = "")
        {
            ViewBag.creatorName = creatorName.Trim();
            ViewBag.productName = productName.Trim();
            DateTime _beginTime = DateTime.Parse("2000-01-01 10:10:10");
            DateTime _endTime = DateTime.Now.Date.AddDays(1);
            ViewBag.beginTime = "";
            ViewBag.endTime = "";
            if (!string.IsNullOrEmpty(beginTime) && Validate.IsDate(beginTime))
            {
                _beginTime = DateTime.Parse(beginTime.Trim());
                ViewBag.beginTime = beginTime.Trim();
            }
            if (!string.IsNullOrEmpty(endTime) && Validate.IsDate(endTime))
            {
                _endTime = DateTime.Parse(endTime.Trim());
                ViewBag.endTime = endTime.Trim();
            }
            IPagedList<ProductTechnologyView> tecjList = _purchaseApp.GetPurList(2, -10, creatorName, productName, _beginTime, _endTime).ToPagedList(page, 10);

            return View(tecjList);
        }

        /// <summary>
        /// </summary>
        /// <param name="pcId"></param>
        /// <returns></returns>
        public ActionResult CheckInfo(Guid pcId)
        {
            PurchaseCompose pc = _purchaseApp.GetSinglePurchaseComposeById(pcId);
            Customer customer = new Customer();
            if (pc != null && pc.SupplierId != null)
            {
                customer = _purchaseApp.GetSingleCustomer(2, pc.SupplierId.Value);
            }
            Product pro = new Product();
            if (pc.Type == 1)
            {
                pro = _technologyApp.GetSingleProduct(pc.ProductId);
            }
            else
            {
                // u = _orderProductApp.GetSinleUserById(p.ApplyId.Value);
            }

            //ViewBag.Order = ord;
            //ViewBag.OrderProduct = op;
            ViewBag.Product = pro;
            ViewBag.Customer = customer;
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            return View(pc);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string UpdatePurchase(PurchaseCompose web)
        {
            PurchaseCompose pc = _purchaseApp.GetSinglePurchaseComposeById(web.Id);
            if (pc == null || pc.SupplierId == null)
            {
                Result.Status = "error";
                Result.Message = "请先维护好供应商";
                return Result.ToJson();
            }
            
            DateTime nowTime = DateTime.Now;
            pc.CreatorId = operatorModel.UserId;
            pc.CreatorName = operatorModel.FamilyName;
            pc.CreateTime = nowTime;
            pc.Status = 3;
            _purchaseApp.UpdatePurchaseCompose(pc);//修改采购单状态

            OperateRecord o = new OperateRecord();
            o.ItemId = web.Id;
            o.TypeId = 17;
            o.EndTime = nowTime;
            o.CreatorId = operatorModel.UserId;
            o.CreatorName = operatorModel.FamilyName;
            _operateRecordApp.UpdateOperateRecord(o);//修改本操作人员工作记录日志


            #region 插入采购打印合并
            if (pc.SupplierId != null && pc.ParentId == null)
            {
                //插入采购打印合并
                PurchasePrintSub ppSubtemp = _purchaseApp.GetSinglePurchasePrintSub(pc.SupplierId.Value, pc.ProductId, pc.CostPrice, DateTime.Now.Date, DateTime.Now.Date.AddDays(1));
                if (ppSubtemp != null)
                {
                    ppSubtemp.Amount = ppSubtemp.Amount + pc.Amount;
                    _purchaseApp.UpdatePurchasePrintSub(ppSubtemp);
                }
                else
                {
                    PurchasePrint pptemp = _purchaseApp.GetSinglePurchasePrint(pc.SupplierId.Value, DateTime.Now.Date, DateTime.Now.Date.AddDays(1));
                    if (pptemp != null)
                    {
                        PurchasePrintSub ppSub = new PurchasePrintSub();//子表
                        ppSub.Id = Guid.NewGuid();
                        ppSub.PurchasePrintId = pptemp.Id;
                        ppSub.SupplierId = pc.SupplierId.Value;
                        ppSub.ProductId = pc.ProductId;
                        ppSub.ProductName = pc.ProductName;
                        ppSub.CreateTime = DateTime.Now;
                        ppSub.Amount = pc.Amount;
                        ppSub.CostPrice = pc.CostPrice;
                        ppSub.OtherId = pc.Id;
                        ppSub.Status = 0;
                        ppSub.Memo = pc.Memo;
                        _purchaseApp.AddPurchasePrintSub(ppSub);
                    }
                    else
                    {
                        PurchasePrint pp = new PurchasePrint();//主表
                        pp.Id = Guid.NewGuid();
                        pp.SupplierId = pc.SupplierId.Value;
                        pp.ProductId = pc.ProductId;
                        pp.ProductName = pc.ProductName;
                        pp.CreateTime = DateTime.Now;
                        pp.Amount = pc.Amount;
                        pp.Status = 0;
                        _purchaseApp.AddPurchasePrint(pp);

                        PurchasePrintSub ppSub = new PurchasePrintSub();//子表
                        ppSub.Id = Guid.NewGuid();
                        ppSub.PurchasePrintId = pp.Id;
                        ppSub.SupplierId = pc.SupplierId.Value;
                        ppSub.ProductId = pc.ProductId;
                        ppSub.ProductName = pc.ProductName;
                        ppSub.CreateTime = DateTime.Now;
                        ppSub.Amount = pc.Amount;
                        ppSub.CostPrice = pc.CostPrice;
                        ppSub.OtherId = pc.Id;
                        ppSub.Status = 0;
                        ppSub.Memo = pc.Memo;
                        _purchaseApp.AddPurchasePrintSub(ppSub);
                    }
                }
            }
            #endregion

            #region 给采购对账插入
            PurchaseReconciliation pr = new PurchaseReconciliation();
            Guid newId = Guid.NewGuid();
            pr.Id = newId;
            pr.PurchaseComposeId = pc.Id;
            pr.Status = -1;
            pr.ApplyTime = nowTime;
            pr.ShouldAmount = pc.Amount;
            pr.ActualAmount = pc.Amount;//web.StandardAmount;
            pr.FinanceNumber = "";
            _purchaseApp.AddPurchaseReconciliation(pr);

            //插入给下一个工作人员添加人工记录
            OperateRecord o1 = new OperateRecord();
            o1.Id = Guid.NewGuid();
            o1.ItemId = newId;//Id
            o1.TypeId = 23;
            o1.StartTime = nowTime;
            o1.EndTime = null;
            o1.CreatorId = null;
            _operateRecordApp.AddOperateRecord(o1);
            #endregion

            //插入给下一个工作人员添加人工记录（采购负责人审核）
            /*OperateRecord o1 = new OperateRecord();
            o1.Id = Guid.NewGuid();
            o1.ItemId = web.Id;//采购单Id
            o1.TypeId = 19;
            o1.StartTime = nowTime;
            o1.EndTime = null;
            o1.CreatorId = null;
            _operateRecordApp.AddOperateRecord(o1);*/

            return Result.ToJson();
        }
    }
}